var http_request = false;
var params;

function toserver(f) {
        var rating;
        var ucomment;

        //gets the value of the selected radio button
        for (var r=0; r < f.rating.length; r++){
                if (f.rating[r].checked){
             rating = f.rating[r].value;
        }
         }

 //        alert(rating);
        //gets the value of the textbox
         ucomment = f.comment.value ;
   //      alert(ucomment);

        var url_val = getURLParam("url");
  //      alert(url_val);
     //   var nym_val = getURLParam("nym");
       // alert(nym_val);

        params = "nym=home&url_val="+url_val+"&rating="+rating+"&comment="+ucomment+"";
//        alert(params);

        var url = "loadXMLDoc(\"http://silo.cs.indiana.edu:13795/servlet/ExplicitRatingServlet?\");";

        setTimeout(url, 0);

}

function loadXMLDoc(url) {

        //alert(url);

        if (window.XMLHttpRequest){
		
         http_request = new XMLHttpRequest();
     //   http_request.onreadystatechange =  function() {  alertContents(http_request); };
        http_request.open('POST', url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(params);
        http_request.onreadystatechange = alertContents;
                      //initialize the request
     }

      else if(window.ActiveXObject){
          //Create our RequestObject
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
          if(http_request){
                http_request.onreadystatechange = function() { alertContents(http_request); };
                http_request.open('POST', url, true);
                http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http_request.send("nym=home&url_val=www.googlenew.com&rating=-2");                      //initialize the request
                }
          }

     if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }


}

function alertContents() {

  //alert("here");
       
        try {

            if (http_request.readyState == 4) {
                //      alert("fine"); 
               if (http_request.status == 200) {
                  //  alert(http_request.responseText);
                    alert("You have rated this website successfully!");
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
        catch( e ) {
             alert('Caught Exception: ' + e);
        }
  }

function format(val) {

        vlength = val.length;
        val = val.slice(1, vlength-1);
        return val;

}

function getURLParam(strParamName){
        var strReturn = "";
        var strHref = window.location.href;
        if ( strHref.indexOf("?") > -1 ){
                var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
                var aQueryString = strQueryString.split("&");
                for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
                        if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
                        var aParam = aQueryString[iParam].split("=");
                        strReturn = aParam[1];
                        break;
                        }
                }

        }

        return strReturn;

}
                                                                                                   
